package com.zky.interceptor;

import com.zky.context.BaseContext;
import com.zky.utils.JWTutils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class JWTinterceptor implements HandlerInterceptor {
    @Autowired
    JWTutils jwTutils;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("JWTinterceptor begin");
        String token = request.getHeader("token");
        log.info("token:{}", token);
            String s = jwTutils.parseToken(token);
            log.info("account:{}", s);
            if(s==null||s.length()==0)
            {
                throw  new Exception("token is null");
            }
            BaseContext.setAccount(s);
            return true;

    }
}
